/**
 *    Copyright (C) 2008  Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */

#ifndef __CUBEFS_CORE_BTREE_ITERATOR_H
#define __CUBEFS_CORE_BTREE_ITERATOR_H



#include <cubefs/core/btree/iterator/api.hpp>
#include <cubefs/core/btree/iterator/model_api.hpp>
#include <cubefs/core/btree/iterator/walk.hpp>
#include <cubefs/core/btree/iterator/tools.hpp>

#include <cubefs/core/btree/iterator/base.hpp>

#include <cubefs/core/btree/names.hpp>

namespace cubefs    {
namespace btree     {

using namespace cubefs::ctr;

template<
        typename Profile,
        typename ModelType
>
class BTreeIterator: 
public cubefs::ctr::Iterator<
            Profile,
            ModelType,
            BTreeIterator<
                Profile,
                ModelType
            >
        >
{
    typedef BTreeIterator<Profile, ModelType>                         			MyType;
    typedef cubefs::ctr::Iterator<Profile, ModelType, MyType>     				Base;


    typedef typename Base::NodeBase                                             NodeBase;
    typedef typename Base::Txn                                                  Txn;

    bool managed_;

public:
    typedef ModelType                                                           Model;
    
    BTreeIterator(Model &model):Base(*this, model), managed_(false) {
        Base::state() = 0;
        Base::SetEof(true);
        Base::SetEmpty(true);
    }
    
    BTreeIterator(NodeBase* node, Int idx, Model &model): Base(*this, model), managed_(false) {
        Base::page() = node;
        Base::key_idx() = idx;
        Base::state() = 0;
    }

    BTreeIterator(const MyType& other): Base(*this, other.model_) {
        managed_ = other.managed_;
        setup(other);
    }


    virtual bool managed() const {
        return managed_;
    }

    void set_managed(bool managed) {
        managed_ = managed;
    }
};

}
}


#endif
